package com.api.gbhg.luoao.web;

import com.alibaba.fastjson.JSON;
import com.api.gbhg.liuwenbo.dao.DataBaseUtil;
import org.jgroups.protocols.SIZE;
import org.json.JSONArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import weaver.conn.RecordSet;
import weaver.general.TimeUtil;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.*;
import java.util.stream.Collectors;


/**
 * 生成支付令
 */
@Path("/PayCommand")
public class PayCommand {

    private static final Logger logger = LoggerFactory.getLogger(PayCommand.class);


    @POST
    @Path("/generate")
    @Produces(MediaType.APPLICATION_JSON)
    public String  generate(Map<String,Object> map){
        Map<String,Object> param = new HashMap<>();

        //查询是否已经存在,存在进行删除
        boolean b = DoesitExist(map.get("requestId").toString());
        if (b){
            //插入数据
            Object detail1 = map.get("detail");
            List<Map<String,Object>> detail =(List<Map<String,Object>>)detail1;
            boolean insert = insertgbxlcdyzfl(detail, map.get("requestId").toString());
            param.put("result",insert);
            return JSON.toJSONString(param);
        }

        param.put("result",b);
        return JSON.toJSONString(param);

    }



    //查询数据是否存在,存在删除
    public static boolean DoesitExist(String requestId){

        RecordSet rs = new RecordSet();
        rs.executeQuery("select 1 from uf_gbxlcdyzfl where bxlcid = "+requestId);
        if (rs.next()){
           return deleterequest(requestId);
        }
        return true;

    }

    //删除以前的数据
    public static  boolean deleterequest(String requestId){
        RecordSet rs = new RecordSet();
        boolean b = rs.executeUpdate("delete from uf_gbxlcdyzfl where bxlcid =" + requestId);
        return  b;
    }


    //插入数据
    public static boolean insertgbxlcdyzfl(List<Map<String,Object>> list,String requestId){
        String currentDateString = TimeUtil.getCurrentDateString();
        String currentTimeString = TimeUtil.getOnlyCurrentTimeString();
        String uuid = UUID.randomUUID().toString();
        Integer modeId = DataBaseUtil.getModeId("uf_gbxlcdyzfl");


        StringBuffer sb = new StringBuffer();
        sb.append("insert into uf_gbxlcdyzfl(bxlcid,bxlcmxxid,dyzfxm,zfl," +
                "modedatacreater,modedatacreatedate,modedatacreatertype,modedatacreatetime,formmodeid,modeuuid) values");

        //插入去重（支付令相同的数据去掉remove）
        for(int i=0;i<list.size();i++){
            for (int j = i+1;j<list.size();j++){
                if(list.get(i).get("zfl").equals(list.get(j).get("zfl"))){
                    list.remove(j);
                    j--;
                }
            }
        }


        for (Map<String,Object> param : list){
            sb.append("('"+requestId+"','"+param.getOrDefault("bxlcmxxid","")+"'" +
                    ","+param.getOrDefault("dyzfxm","")+"," +
                    "'"+param.getOrDefault("zfl","")+"'" +
                    ",1,'"+currentDateString+"',0,'"+currentTimeString+"',"+modeId+",'"+uuid+"'),");
        }


        sb.deleteCharAt(sb.length()-1);

        RecordSet rs = new RecordSet();
        logger.info("生成支付令sql打印："+sb);
        return rs.executeUpdate(sb.toString());
    }

}
